﻿using DotNet.Collections;
using DotNet.Data;
using DotNet.Extensions;
using DotNet.Helper;
using DotNet.JCore.Entity;
using DotNet.Utility;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;


namespace DotNet.JCore.Service
{
    /// <summary>
    /// 车辆稽查点关联管理服务
    /// </summary>
    public class CarPointMapServer
    {
        private JCoreRepository<CarPointMap> repos = new JCoreRepository<CarPointMap>();

   
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="isCreate">是否新增</param>
        public BoolMessage Save(CarPointMap entity, bool isCreate)
        {
            
            entity.CarNumber = entity.CarNumber.ToUpper();

            var result = EntityHelper.ValidAsMessage(entity, "<br>");
            if (result.Failure)
            {
                return result;
            }

            result = IsVehicleNumber(entity.CarNumber);
            if (result.Failure)
            {
                return result;
            }


            if (isCreate)
            {
                repos.Insert(entity);
            }
            else
            {
                repos.UpdateExclude(entity, p => p.CreateId, p => p.CreateName, p => p.CreateTime);
            }
            return BoolMessage.True;
        }


        /// <summary>
        /// 车号校验
        /// </summary>
        /// <param name="vehicleNumber"></param>
        /// <returns></returns>
        public BoolMessage IsVehicleNumber(string vehicleNumber)
        {

            string express = @"^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$";

            if (Regex.IsMatch(vehicleNumber, express))
            {
                return BoolMessage.True;
            }
            else
            {
                return new BoolMessage(false, $"车号 {vehicleNumber} 不符合规范，请重新输入！");
            }

        }


        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="ids">主键数组</param>
        public BoolMessage Delete(string[] ids)
        {
            repos.Delete(ids);
            return BoolMessage.True;
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="id">主键</param>
        public CarPointMap Get(string id)
        {
            return repos.Get(id);
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="userId">账号主键</param>
        public CarPointMap GetById(string Id)
        {
            return repos.Get(p => p.Id == Id);
        }

        /// <summary>
        /// 获取启用的简单对象集合(已排序)
        /// </summary>
        public List<Simple> GetSimpleList()
        {
            var query = repos.SQL
                .OrderByDesc(p => p.PointName)
                .OrderByDesc(p => p.Station)
                .Select(p => p.Id, p => p.CarNumber);

            return repos.Fetch(query).
                Select(p => new Simple(p.Id, p.CarNumber, SpellHelper.GetSpell(p.CarNumber)))
                .ToList();
        }

        /// <summary>
        /// 获取启用的对象集合(已排序)
        /// </summary>
        /// <param name="carnumber">车号关键字</param>
        /// <param name="pointId">检查点</param>
        public List<CarPointMap> GetList(string carnumber, string pointId,string carcolor,string stationid)
        {
            // var query = repos.SQL.OrderByDesc(p => p.PointName);
            var query = repos.SQL;
            SetQuery(query, carnumber, pointId, carcolor,stationid);
            return repos.Fetch(query);
        }

        /// <summary>
        /// 获取对象分页集合
        /// </summary>
        /// <param name="pageCondition">分页对象</param>
        /// <param name="carnumber">名称关键字</param>
        /// <param name="pointId">检查点</param>
        public PageList<CarPointMap> GetPageList(PaginationCondition pageCondition,
            string carnumber, string pointId,string carcolor,string stationid)
        {
            pageCondition.SetDefaultOrder(nameof(CarPointMap.PointName));
            var query = repos.PageQuery(pageCondition);
            SetQuery(query, carnumber, pointId, carcolor,stationid);
            query.ExcludeSelect(p => p.Note);
            return repos.Page(query);
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="query">查询对象</param>
        /// <param name="carnumber">数据对象</param>
        public void SetQuery(SQLQuery<CarPointMap> query, string carnumber, string pointId, string carcolor,string stationid)
        {
            query.OrderByDesc(p => p.PointName);
            query.OrderByDesc(p => p.Station);
            if (pointId.IsNotEmpty())
            {
                pointId = pointId.Trim();
                query.Where(p => p.PointId == pointId);
            }
            if (carcolor.IsNotEmpty())
            {
                carcolor = carcolor.Trim();
                query.Where(p => p.CarColor == carcolor);
            }
            if (stationid.IsNotEmpty())
            {
                stationid = stationid.Trim();
                query.Where(p => p.StationID == stationid);
            }

            if (carnumber.IsNotEmpty())
            {
                carnumber = carnumber.Trim();
                query.Where(p => p.CarNumber.Contains(carnumber)
                    || p.Line.Contains(carnumber)
                    || p.Corp.Contains(carnumber));
            }
        }





    }
}
